using JSG.BubbleShoot.ScriptableObjects;
using JSG.Car.ScriptableObjects;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Playables;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace JSG.BubbleShoot.UI
{
    public class GameUI : MonoBehaviour
    {

        public Text[] m_PowerCountTexts;


        public Sprite[] m_ColorSprites;

        public Image m_BallImage1;
        public Image m_BallImage2;

        public Image m_SwitchButton;

        public Image m_TouchArea;

        public Text m_BallCountText;

        public Text m_LevelText;
        [SerializeField, Space]
        private DataStorage m_DataStorage;
        [SerializeField, Space]
        private GameData m_GameData;

        // Start is called before the first frame update
        void Start()
        {
            m_LevelText.text = "Level" + " " + ((m_DataStorage.Level) + 1).ToString();

            m_TouchArea.Raycast(Input.mousePosition, Camera.main);
        }

        // Update is called once per frame
        void Update()
        {
            for (int i = 0; i < 3; i++)
            {
                m_PowerCountTexts[i].text = m_DataStorage.m_PowerCounts[i].ToString();
            }

            m_BallCountText.text = GameControl.m_Current.m_BallCount.ToString();

            if (GameControl.m_Current.m_CurrentColor >= 0)
            {
                m_BallImage1.sprite = m_ColorSprites[GameControl.m_Current.m_CurrentColor];
            }

            if (GameControl.m_Current.m_NextColor >= 0)
            {
                m_SwitchButton.gameObject.SetActive(true);
                m_BallImage2.gameObject.SetActive(true);
                m_BallImage2.sprite = m_ColorSprites[GameControl.m_Current.m_NextColor];
            }
            else
            {
                m_SwitchButton.gameObject.SetActive(false);
                m_BallImage2.gameObject.SetActive(false);
            }



            if (Input.touchCount > 0)
            {
                Vector2 pos = Input.touches[0].position;
                CheckInput(pos);
            }

            if (Input.GetMouseButtonDown(0))
            {
                CheckInput(Input.mousePosition);
            }

        }

        public void CheckInput(Vector2 pos)
        {


            PointerEventData data = new PointerEventData(EventSystem.current);
            data.position = Input.mousePosition;

            List<RaycastResult> raycasts = new List<RaycastResult>();

            EventSystem.current.RaycastAll(data, raycasts);

            if (raycasts.Count > 0)
            {
                foreach (RaycastResult raycast in raycasts)
                {
                    if (raycast.gameObject == m_TouchArea.gameObject)
                    {
                        GameControl.m_Current.HandleInputShoot();
                        break;
                    }
                }
            }

        }

        public void BtnPower(int num)
        {
            //  if (m_DataStorage.m_PowerCounts[num] > 0)
            //  {
            GameControl.m_Current.SelectPower(num);
            // }
            //  else
            //  {
            //    m_GameData.m_PowerSelectNum = num;
            //    m_VideoPowerPanel.gameObject.SetActive(true);
            //}


        }

        public void BtnSwitch()
        {
            if (GameControl.m_Current.m_State == 1)
            {
                GameControl.m_Current.SwitchColor();
            }
        }

        public void BtnRestart()
        {
            Invoke("ReloadScene", 2);

        }

        public void BtnBack()
        {
            Invoke("LoadMainScene", 2);
        }
        public void ReloadScene()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
        public void LoadMainScene()
        {
            SceneManager.LoadScene("MainMenu");
        }

    }
}